using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Hotran.SaveSystem
{
    /// <summary>
    /// ���ɳ��������п�ȱID
    /// </summary>
    public class SavableIdHelper : MonoBehaviour
    {
#if UNITY_EDITOR
        // Start is called before the first frame update
        void Start()
        {
            CheckIDs();
        }
#endif
        public void CheckIDs()
        {
#if UNITY_EDITOR
            HashSet<string> ids = new HashSet<string>();
            foreach (var obj in FindObjectsOfType<SavableEntity>())
            {
                if (obj.Id == string.Empty)
                    Debug.LogError($"{obj.name}'s id is empty!", obj.gameObject);
                if (ids.Add(obj.Id) == false)
                    Debug.LogError($"{obj.name}'s id already exists");
            }
            ids.Clear();
#endif
        }

        [ContextMenu("Generate All IDs")]
        public void GenerateAllIds()
        {
#if UNITY_EDITOR
            foreach (var obj in FindObjectsOfType<SavableEntity>())
            {
                GenerateId(obj);
            }
#endif
        }

        private void GenerateId(IIdentifiable obj)
        {
            if (obj.Id == string.Empty)
            {
                obj.GenerateId();               
            }
        }
    }
}